package com.blobfish.fiction.common.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.blobfish.fiction.common.base.model.BaseEntityDto;
import com.blobfish.fiction.common.base.model.QmResultTable;
import com.blobfish.fiction.common.base.service.IBaseService;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.util.ArrayList;
import java.util.List;

/**
 * service 基础类
 *
 * @author huangbo
 */
public class BaseServiceImpl<T extends Model, DTO extends BaseEntityDto> implements IBaseService<T, DTO> {

    protected QmResultTable<DTO> renderTableResult(Page<Record> searchResult, Class<DTO> clazz) {
        QmResultTable<DTO> result = new QmResultTable<>();
        List<DTO> resultList = new ArrayList();
        result.setCount(searchResult.getTotalRow());
        result.setCode(0);
        result.setData(resultList);
        for (Record item : searchResult.getList()) {
            resultList.add(BeanUtil.mapToBean(item.getColumns(), clazz, true));
        }
        return result;
    }
}
